package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.ComPlainType;
import com.website.system.model.Complain;
import com.website.system.service.ComPlainTypeService;
import com.website.system.service.ComplainService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/complainType")
public class ComplainTypeController extends BaseController {
    @Resource
    private ComPlainTypeService comPlainTypeService;
    @Resource
    private ComplainService complainService;
    /**
     * 跳转到 投诉类别管理
     */
    @RequestMapping("")
    public String complainType() {
        return "manage/complainType";
    }

    /**
     * 加载数据 投诉类别信息
     */
    @RequestMapping("/complainTypeTable")
    @ResponseBody
    public ResultMap complainTypeTable(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required =false) String name) {
        ResultMap<List<ComPlainType>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Wrapper<ComPlainType> wrapper = new EntityWrapper<>();
        wrapper = name == null ? wrapper : wrapper.like("name", name);
        Page<ComPlainType> pageInfo = new Page<>(page,limit);

        List<ComPlainType> complainList = comPlainTypeService.selectPage(pageInfo,wrapper).getRecords();
        resultMap.setCount(comPlainTypeService.selectCount(wrapper));
        resultMap.setData(complainList);
        return  resultMap;
    }

    /**
     * 添加页面 投诉类别信息
     */
    @RequestMapping("/toComplainTypeAdd")
    public String toComplainTypeAdd(){
        return  "manage/complainTypeAdd";
    }

    /**
     * 保存添加 投诉类别信息
     */
    @ResponseBody
    @RequestMapping("/complainTypeAdd")
    public String complainAdd(@RequestParam String name) {
            try{
                ComPlainType comPlainType = new ComPlainType();
                comPlainType.setName(name);
                comPlainTypeService.insert(comPlainType);
                return SUCCESS_MSG;
            }catch (Exception e){
                return ERROR_MSG;
            }
    }
    /**
     * 查看页面 投诉管理信息
     */
    @RequestMapping("/toComplainTypeRead/{type}")
    public String complainTypeRead(Model model, @PathVariable Integer type) {
        ComPlainType comPlainType = comPlainTypeService.selectById(type);
        model.addAttribute("comPlainType", comPlainType);
        return "manage/complainTypeRead";
    }

    /**
     * 编辑页面 投诉管理信息
     */
    @RequestMapping("/toComplainTypeUpdate/{type}")
    public String toComplainTypeUpdate(Model model, @PathVariable Integer type) {
        ComPlainType complainType = comPlainTypeService.selectById(type);
        model.addAttribute("complainType", complainType);
        return "manage/complainTypeUpdate";
    }

    /**
     * 保存修改 投诉管理信息
     */
    @ResponseBody
    @RequestMapping("/complainTypeUpdate")
    public String complainUpdate(@RequestParam Integer id, @RequestParam String name) {
        try{
            ComPlainType comPlainType = comPlainTypeService.selectById(id);
            comPlainType.setId(id);
            comPlainType.setName(name);
            comPlainTypeService.updateById(comPlainType);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 删除数据 投诉类别信息
     */
    @ResponseBody
    @RequestMapping("/complainTypeDel")
    public String complainTypeDel(@RequestParam Integer id) {
        Wrapper<Complain> wrapper = new EntityWrapper<>();
        wrapper = id == null ? wrapper : wrapper.eq("complaints_type", id);
        List<Complain> complainList = complainService.selectList(wrapper);
        if(complainList.size()==0){
            try{
                comPlainTypeService.deleteById(id);
                return SUCCESS_MSG;
            }catch (Exception e){
                return ERROR_MSG;
            }
        }else{
            return "删除失败！请将该类型的投诉信息删除后再执行该操作";
        }

    }
}
